package com.example.loganalysis.service;

import com.example.loganalysis.model.LogData;
import com.example.loganalysis.model.TokenResponse;
import com.example.loganalysis.utils.HttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class JumpServerService {

    @Value("${jumpserver.api.token}")
    private String tokenUrl;

    @Value("${jumpserver.api.loginLogs}")
    private String loginLogsUrl;

    @Value("${jumpserver.api.operateLogs}")
    private String operateLogsUrl;

    private final RestTemplate restTemplate;
    private final HttpClient httpClient;

    public JumpServerService(RestTemplate restTemplate, HttpClient httpClient) {
        this.restTemplate = restTemplate;
        this.httpClient = httpClient;
    }

    public String getToken(String username, String password) {
        TokenResponse tokenResponse = restTemplate.postForObject(
                tokenUrl, new TokenRequest(username, password), TokenResponse.class);
        return tokenResponse != null ? tokenResponse.getToken() : null;
    }

    public LogData[] getLoginLogs(String token) {
        return httpClient.getJsonData(loginLogsUrl, token, LogData[].class);
    }

    public LogData[] getOperateLogs(String token) {
        return httpClient.getJsonData(operateLogsUrl, token, LogData[].class);
    }
}
